package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.system.dao.SysUserColumnResDao;
import com.ccp.dev.system.model.SysUserColumnRes;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 首页用户栏目内容 信息操作服务类
 *
 * @author huangyan
 * @date 2019-07-25 08:59:55
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserColumnResService extends BaseService<SysUserColumnRes> {

    @Resource
    private SysUserColumnResDao dao;

    /**
     * 获取用户的应用
     * @author huangyan
     * @date 2019/7/25
     * @param userId 用户id
     * @param columnAlias 栏目别名
    */
    public List<Map<String, Object>> getUserRes(String userId, String columnAlias) {
        return dao.getUserRes(userId, columnAlias);
    }

    /**
     * 获取非用户应用
     * @author huangyan
     * @date 2019/7/25
     * @param systemId 子系统id
     * @param roleId 角色id
     * @param userId 用户id
     * @param columnAlias 栏目别名
    */
    public List<Map<String, Object>> getNonUserRes(String systemId,  String roleId, String userId, String columnAlias) {
        return dao.getNonUserRes(systemId, roleId, userId, columnAlias);
    }

    /**
     * 首页保存我的应用
     * @author huangyan
     * @date 2019/7/25
     * @param userId 用户id
     * @param columnAlias 栏目别名
     * @param addResIdList 要新增的应用id
     * @param delResIdList 要删除的应用id
     * @return void
     */
    public void saveUserColRes(String userId, String columnAlias, List<String> addResIdList, List<String> delResIdList) throws Exception {
        List<SysUserColumnRes> newList = new ArrayList<>();
        for (String resId : addResIdList) {
            SysUserColumnRes columnRes = new SysUserColumnRes();
            columnRes.setUserColResId(UUIDUtils.getUUIDFor32());
            columnRes.setColumnAlias(columnAlias);
            columnRes.setResid(resId);
            columnRes.setUserid(userId);

            newList.add(columnRes);
        }
        //批量插入
        if (newList.size() > 0) {
            dao.addList(newList);
        }

        List<SysUserColumnRes> delList = new ArrayList<>();
        for (String resId : delResIdList) {
            SysUserColumnRes columnRes = new SysUserColumnRes();
            columnRes.setColumnAlias(columnAlias);
            columnRes.setResid(resId);
            columnRes.setUserid(userId);

            delList.add(columnRes);
        }

        //批量删除
        if (delList.size() > 0) {
            dao.delList(delList);
        }
    }
}
